import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author sk.z
 * 2023/10/7
 * 注意：Runtime.exec() 不是cmd或shell环境，因此无法直接调用dir等命令，
 * 需要在程序中读取运行的操作系统平台，以调用不同的命令解释器
 * （NT：cmd.exe，windows 95/98：command.exe，linux：/bin/sh）
 **/
public class runtime_exec {

    public static void main(String[] args) throws IOException, InterruptedException {
        String os = System.getProperty("os.name");
        System.out.println(os);
        if (!os.toLowerCase().contains("window")) {
            System.out.println("this program not support " + os);
            System.exit(0);
        }
        Process process = Runtime.getRuntime().exec("cmd /c dir");
        System.out.println("---------------------------------------------------------------------");
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "gbk"));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        System.out.println("---------------------------------------------------------------------");
        int exitValue = process.waitFor();
        System.out.println(exitValue == 0 ? "success" : "fail");
    }
}
